//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package org_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/org_forum_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type ForumThreadInfoDbItem struct {
	ThreadId       string `bson:"_id"`
	OrgId          string `bson:"orgId"`
	ForumId        string `bson:"forumId"`
	Title          string `bson:"title"`
	Weight         uint32 `bson:"weight"`
	FirstContentId string `bson:"firstContentId"`
	UserId         string `bson:"userId"`
	CreateTime     int64  `bson:"createTime"`
	UpdateTime     int64  `bson:"updateTime"`
	ContentCount   uint32 `bson:"contentCount"`
}

func (item *ForumThreadInfoDbItem) ToForumThreadInfo() *org_forum_api.ForumThreadInfo {
	return &org_forum_api.ForumThreadInfo{
		ThreadId:       item.ThreadId,
		ForumId:        item.ForumId,
		Title:          item.Title,
		Weight:         item.Weight,
		FirstContentId: item.FirstContentId,
		UserId:         item.UserId,
		CreateTime:     item.CreateTime,
		UpdateTime:     item.UpdateTime,
		ContentCount:   item.ContentCount,
	}
}

type _ForumThreadDao struct {
	mc *mongo.Collection
}

func newForumThreadDao(db *mongo.Database) (*_ForumThreadDao, error) {
	mc := db.Collection(db_ns.DB_NS_ORG + "forum_thread")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"orgId", 1}, {"forumId", 1}, {"weight", 1}, {"createTime", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_ForumThreadDao{mc: mc}, nil
}

func (db *_ForumThreadDao) Insert(ctx context.Context, dbItem *ForumThreadInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_ForumThreadDao) Exist(ctx context.Context, orgId, forumId, threadId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"orgId": orgId, "forumId": forumId, "_id": threadId})
	return count > 0, err
}

func (db *_ForumThreadDao) Get(ctx context.Context, orgId, forumId, threadId string) (*ForumThreadInfoDbItem, error) {
	retItem := &ForumThreadInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"orgId": orgId, "forumId": forumId, "_id": threadId}).Decode(retItem)
	return retItem, err
}

func (db *_ForumThreadDao) Remove(ctx context.Context, orgId, forumId, threadId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"orgId": orgId, "forumId": forumId, "_id": threadId})
	return err
}

func (db *_ForumThreadDao) RemoveByForum(ctx context.Context, orgId, forumId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"orgId": orgId, "forumId": forumId})
	return err
}

func (db *_ForumThreadDao) RemoveByOrg(ctx context.Context, orgId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"orgId": orgId})
	return err
}

func (db *_ForumThreadDao) Count(ctx context.Context, orgId, forumId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"orgId": orgId, "forumId": forumId})
	return uint32(count), err
}

func (db *_ForumThreadDao) List(ctx context.Context, orgId, forumId string, offset, limit int64) ([]*ForumThreadInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"orgId": orgId, "forumId": forumId},
		options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.D{{"weight", -1}, {"createTime", -1}}))
	if err != nil {
		return nil, err
	}
	retList := []*ForumThreadInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_ForumThreadDao) ListById(ctx context.Context, orgId string, threadIdList []string) ([]*ForumThreadInfoDbItem, error) {
	if len(threadIdList) == 0 {
		return []*ForumThreadInfoDbItem{}, nil
	}
	cursor, err := db.mc.Find(ctx, bson.M{"orgId": orgId, "_id": bson.M{"$in": threadIdList}})
	if err != nil {
		return nil, err
	}
	retList := []*ForumThreadInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_ForumThreadDao) Update(ctx context.Context, orgId, forumId, threadId, title string, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"orgId": orgId, "forumId": forumId, "_id": threadId},
		bson.M{"$set": bson.M{
			"title":      title,
			"updateTime": updateTime,
		}})
	return err
}

func (db *_ForumThreadDao) UpdateWeight(ctx context.Context, orgId, forumId, threadId string, weight uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"orgId": orgId, "forumId": forumId, "_id": threadId},
		bson.M{"$set": bson.M{
			"weight": weight,
		}})
	return err
}

func (db *_ForumThreadDao) UpdateContentCount(ctx context.Context, orgId, forumId, threadId string, count uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"orgId": orgId, "forumId": forumId, "_id": threadId},
		bson.M{"$set": bson.M{
			"contentCount": count,
		}})
	return err
}
